<?php

namespace Home\Controller;

use Home\Controller\BaseController;

class UserController extends BaseController {

    public function _initialize() {
        parent::_initialize();
        if (!$this->user_id) {
            $this->error('请先登录');
        }
    }

    //用户余额
    public function userBalance() {
        $total_amount = M('user')->where(['user_id' => $this->user_id])->getField('total_amount');
        $this->returnList($total_amount);
    }

    //用户信息
    public function userInfo() {
        $userInfo = M('user')->where(['user_id' => $this->user_id])->find();
        $userInfo['headpic'] = subOssImgUrl($userInfo['headpic']);
        $this->returnList($userInfo);
    }

    //用户编辑
    public function updateUserinfo() {
        $user_phone = $this->getParam('user_phone', '', 'string', TRUE, 'user_phone必须');
        $nickname = $this->getParam('nickname');
        !empty($nickname) && $data['nickname'] = $nickname;
//        $headpic = $this->uploadFileBase64($_POST['headpic'],'Home');
//        !empty($headpic) && $data['headpic'] = $headpic;
        !empty($_POST['headpic']) && $data['headpic'] = $_POST['headpic'];
        !empty($_POST['zan']) && $data['zan'] = $_POST['zan'];
        !empty($_POST['cai']) && $data['cai'] = $_POST['cai'];
        !empty($_POST['remark']) && $data['remark'] = $_POST['remark'];
        
        
//        $res = M('user')->where(['user_id' => $this->user_id])->save($data);
        $res = M('user')->where(['user_phone' => $user_phone])->save($data);
        $this->success('修改成功');
    }

    //设置密码
    public function setPaypsw() {
        $password = $this->getParam('password', '', TRUE);
        $res = M('user')->where(['user_id' => $this->user_id])->save(['password' => md5($password)]);
        if ($res) {
            $this->success('修改成功');
        } else {
            $this->error('修改失败');
        }
    }

    //添加收藏
    public function addCollection() {
        $wid = $this->getParam('wid', 0, 'int+', TRUE, '选择商品ID');
        $where['user_id'] = $this->user_id;
        $where['wid'] = $wid;
        $collectionInfo = M('user_collection')->where($where)->find();
        if ($collectionInfo) {
            $this->error('已收藏');
        }
        $data = $where;
        $res = M('user_collection')->add($data);
        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    //取消收藏
    public function cancelCollection() {
        $wid = $this->getParam('wid', 0, 'int+', TRUE, '选择商品ID');
        $where['user_id'] = $this->user_id;
        $where['wid'] = $wid;
        $res = M('user_collection')->where($where)->delete();
        $this->success();
    }

    //收藏列表
    public function collectionList() {
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $where['uc.user_id'] = $this->user_id;
        $where['s.state'] = 1;
        $sort = 'uc.id DESC';
        $list = M()->table('ot_user_collection uc')
                ->field("s.wid,s.nickname,s.headpic,s.age,s.height,s.weight,s.bust,s.price,s.xingzuo,s.auth_state,s.old_address,s.address,s.detail_address,s.ishome,s.update_time,s.create_time,s.isonwork,s.state,s.intro,s.lon,s.lat")
                ->join('ot_seller as s ON s.wid = uc.wid')
                ->where($where)
                ->order($sort)
                ->limit($size)
                ->page($page)
                ->select();
        foreach ($list as &$val) {
            $val['headpic'] = subOssImgUrl($val['headpic']);
        }
        $this->returnList($list);
    }

    //退出登录
    public function logout() {
        $user_id = $this->user_id;
        $this->success();
    }

    //下单
    public function createOrder() {
        $wid = $this->getParam('wid', 0, 'int+', TRUE, '选择商品ID');
        $data = I('date');
        $phone = $this->getParam('phone', '', 'string', TRUE);
        $home_address = $this->getParam('home_address');
        //商品信息
        $sellerInfo = M('seller')->where(['wid' => $wid])->find();
        if (!$sellerInfo) {
            $this->error('商品不存在');
        }
        $data['order_no'] = '1' . date('YmdHis') . rand(1000, 9999);
        $data['user_id'] = $this->user_id;
        $data['wid'] = $wid;
        $data['phone'] = $phone;
        $data['home_address'] = $home_address;
        $data['order_price'] = $sellerInfo['price'];
        $data['state'] = 1;
        $order_id = M('order')->add($data);
        if ($order_id) {
            $this->returnList(['order_id' => $order_id]);
        } else {
            $this->error();
        }
    }

    //订单列表
    public function orderList() {
        $postData = $_POST;
        $page = intval($postData['page']) ? intval($postData['page']) : 1;
        $size = intval($postData['size']) ? intval($postData['size']) : 10;
        $list = M('order')
                ->alias('o')
                ->field('o.*, s.nickname,s.headpic')
                ->join('ot_seller AS s ON s.wid = o.wid')
                ->where(['o.user_id' => $this->user_id])
                ->order('o.order_id DESC')
                ->limit($size)
                ->page($page)
                ->select();
        $this->returnList($list);
    }

    //订单详情
    public function orderDetail() {
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE, '请选择订单');
        $where['order_id'] = $order_id;
        $where['user_id'] = $this->user_id;
        $orderInfo = M('order')
                ->alias('o')
                ->field('o.*, s.nickname,s.headpic,s.lon,s.lat,s.isonwork')
                ->join('ot_seller AS s ON s.wid = o.wid')
                ->where($where)
                ->find();
        $this->success();
    }

    //订单支付
    public function paySuccess() {
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE, '请选择订单');
        $where['order_id'] = $order_id;
        $where['user_id'] = $this->user_id;
        $res = M('order')->where($where)->save(['state' => 3]);
        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    //取消订单
    public function cancelOrder() {
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE, '请选择订单');
        $where['order_id'] = $order_id;
        $where['user_id'] = $this->user_id;
        $res = M('order')->where($where)->save(['state' => 5]);
        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    //线下支付
    public function offlinePay() {
        $order_id = $this->getParam('order_id', 0, 'int+', TRUE, '请选择订单');
        $where['order_id'] = $order_id;
        $where['user_id'] = $this->user_id;
        $res = M('order')->where($where)->save(['state' => 3]);
        if ($res) {
            $this->success();
        } else {
            $this->error();
        }
    }

    //评论
    public function publicEvalution() {
        $wid = $this->getParam('wid', 0, 'int+', TRUE, "wid必须");
        $content = $this->getParam('content');
        $this->_authSign($wid.$content);
        
        
        //白名单的商家不限制
        $arrWhiteId = [618];
        $user_id = $this->user_id;
        $order_id = $this->getParam('order_id', 0, 'int+');
        $wid = $this->getParam('wid', 0, 'int+');
        $did = $this->getParam("did");
        empty($did) && $this->error("did参数必须");
        
        if(!in_array($wid, $arrWhiteId)){
            $evalCountDay = M('order_eval')->where(['wid' => $wid, 'did' => $did, 'create_time'=>array("like", date("Y-m-d")."%")])->count();
            if($evalCountDay > 0){
                $this->error('您已评价，无需重复评论');
            }
            $evalCountAll = M('order_eval')->where(['wid' => $wid, 'did' => $did])->count();
            if($evalCountAll > 3){
                $this->error('该用户已经评价超过上限');
            }
            
            if ($order_id) {
                //是否已评论
                $evalCount = M('order_eval')->where(['wid' => $wid, 'did' => $did])->count();
                if ($evalCount) {
                    $this->error('您已评论，无需重复评论');
                }
                $wid = M('order')->where(['order_id' => $order_id])->getField('wid');
            }
        }

        $data['order_id'] = intval($order_id);
        $data['wid'] = $wid;
        $data['user_id'] = $user_id;
        $data['content'] = $this->getParam('content');
        $data['attitude_star'] = $this->getParam('attitude_star', 0);
        $data['face_star'] = $this->getParam('face_star', 0);
        $data['did'] = $did;
        $eval_id = M('order_eval')->add($data);
        if ($eval_id) {
            if ($order_id) {
                M('order')->where(['order_id' => $order_id])->save(['state' => 4]);
            }
            $this->success();
        } else {
            $this->error();
        }
    }

}
